package com.lancoo.answer.helper;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.ExamViewBean;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.answer.widget.combinationView.FillContextAnswerResultView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/24 15:54
 * Description:短语语境查看评阅弹窗
 */
public class PhrasalContextItemResultDialog extends DialogFragment {

    private static final String TAG = "PhrasalContextItemResul";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL,R.style.ev_dialogStyle);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_dialog_layout_phrasal_context_result,container,false);
    }

    public static void launchDialog(FragmentManager fragmentManager,int typeIndex,
                                    int quesIndex,int childIndex,int itemIndex){
        PhrasalContextItemResultDialog fragment=new PhrasalContextItemResultDialog();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,typeIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,quesIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX,childIndex);
        mBundle.putInt(AnswerConstant.KEY_ITEMINDEX,itemIndex);
        fragment.setArguments(mBundle);
        fragment.show(fragmentManager,TAG);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null&& getActivity()!=null&&dialog.getWindow()!=null&&getContext()!=null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            int dp12= DpUtils.dip2px(getContext(),12);
            int dp200=DpUtils.dip2px(getContext(),320);
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.dimAmount=0.05f;
            attributes.width=dm.widthPixels-dp12*2;
            attributes.height=dp200;
            dialog.setCancelable(false);
            window.setAttributes(attributes);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(getArguments()==null){
            return;
        }
        FillContextAnswerResultView fillContextAnswerResultView=view.findViewById(R.id.fill_context_answer);
        int typeIndex=getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int quesIndex=getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int childIndex=getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        int itemIndex=getArguments().getInt(AnswerConstant.KEY_ITEMINDEX);
        ExamViewBean constantExamBean = ConstantBean.Companion.getConstantExamBean();
        if(constantExamBean==null){
            return;
        }
        List<Type> typeList = constantExamBean.getTypeList();
        if(typeList==null){
            return;
        }
        List<Ques> quesList = typeList.get(typeIndex).getQuesList();
        if(quesList==null){
            return;
        }
        Ques ques = quesList.get(quesIndex);
        String typeNo = ques.getTypeNo();
        List<Child> childList = ques.getChildList();
        if(childList==null){
            return;
        }
        List<Item> itemList = childList.get(childIndex).getItemList();
        if(itemList==null){
            return;
        }
        Item item=itemList.get(itemIndex);
        fillContextAnswerResultView.setFillResult(item);
        view.findViewById(R.id.ll_close).setOnClickListener(v -> dismissAllowingStateLoss());
        if(TextUtils.equals("m",typeNo)){//匹配题只显示答案解析
            fillContextAnswerResultView.onlyShowAnalysis();
        }
    }
}
